package com.example.demo.module.DTO;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSONUtil;
import com.example.demo.constant.BaseConstant;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.HashMap;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class DatasourceDto {

    private Long dsId;

    private Long userId;

    private Long orgId;

    private String dsName;

    private String dsType;

    private String typeDetil;

    private String ifValid;

    private String theDesc;

    private Integer tableCount;

    private String url;

    private String driver;

    private String username;

    private String password;

    private String prefixWhite;

    private String prefixBlack;

    private String suffixWhite;

    private String suffixBlack;

    private String regularFilter;

    /**
     * KAFKA相关
     */
    private String groupId;

    private String topic;

    private String enableAutoCommit;

    private String autoOffsetReset;

    private String connectTime;

    private String businessCode;

    private String schemaName;

    public Datasource validateAndConverterToDatasource() {
        Datasource datasource = BeanUtil.copyProperties(this, Datasource.class);
        HashMap<String, String> map = new HashMap<>();
        map.put("url", this.url);
        switch (this.getTypeDetil()) {
            case BaseConstant.MYSQL:
            case BaseConstant.ORACLE:
            case BaseConstant.POSTGRES:
            case BaseConstant.DM: {
                map.put("type", this.typeDetil);
                map.put("driver", this.driver);
                map.put("username", this.username);
                map.put("password", this.password);
                map.put("prefixWhite", this.prefixWhite);
                map.put("prefixBlack", this.prefixBlack);
                map.put("suffixWhite", this.suffixWhite);
                map.put("suffixBlack", this.suffixBlack);
                map.put("regularFilter", this.regularFilter);
                datasource.setDatasourceJson(JSONUtil.toJsonStr(map));
                return datasource;
            }
            case BaseConstant.HDFS: {
                datasource.setDatasourceJson(JSONUtil.toJsonStr(map));
                return datasource;
            }
            case BaseConstant.KAFKA: {
                map.put("topic", this.topic);
                map.put("enableAutoCommit", this.enableAutoCommit);
                map.put("autoOffsetReset", this.autoOffsetReset);
                map.put("connectTime", this.connectTime);
                datasource.setDatasourceJson(JSONUtil.toJsonStr(map));
                return datasource;
            }
            default: {
                return datasource;
            }
        }
    }

}
